<?php

namespace Sbm;

class ProfessionSearch extends ObjectSearch
{

	function GetDefaultFilters()
	{
		return array(
			'select' => "`profession`.*",
			'from' => "`{profession}` as `profession`",
		);
	}

	function GetByName( $name )
	{
		return $this->GetByFilters(array(
					'where' => "`profession`.`title` = '" . $this->db->Escape($name) . "'",
				));
	}

	/**
	 * Возвращает список Profession по массиву $master_ids
	 * В результате возвращается массив array(master_id1=>array(Profession1,Profession2,...), master_id2=>array(Profession2,...),...)
	 *   
	 * @param $master_ids
	 * @return array of array
	 */
	function GetByMasters( $master_ids )
	{
		foreach ($master_ids as $v)
		{
			$result[$v] = array();
		}
		$s = "SELECT 
		             `ink`.`master_id`,
		             `profession`.* 
		      FROM 
		             `{master_profession}` as `ink` LEFT JOIN
		             `{profession}` as `profession` ON `ink`.`profession_id` = `profession`.`id`
		      WHERE 
		             `ink`.`master_id` IN ('" . implode("','", $master_ids) . "')";
		$q = $this->db->Query($s);
		$z = array();
		while ($r = $q->FetchRow())
		{
			$pid = $r['id'];
			$master_id = $r['master_id'];
			if ( !isset($z[$pid]) )
			{
				$z[$pid] = new Profession(array(
							'id' => $r['id'],
							'title' => $r['title']
						));
			}
			$result[$master_id][] = &$z[$pid];
		}
		return $result;
	}

	/**
	 * (non-PHPdoc)
	 * @see includes/ObjectSearch#CreateOneItem($init_array)
	 */
	protected function CreateOneItem( $r )
	{
		return new Profession($r);
	}

}